package com.isscollage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.isscollage.entity.Users;

public class SessionIntercepter extends HandlerInterceptorAdapter /* implements HandlerInterceptor*/ {

	/**
	 * 通过url。执行某个方法之前。调用该方法
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("===========拦截器开始start=============");
		Users user = (Users) request.getSession().getAttribute("user");
		if(user==null || "".equals(user.getUserId())) {
			response.sendRedirect(request.getContextPath()+"/login/toLogin");
		} else{
			return true;
		}
		return false;
	}
	/**
	 * 通过url。执行某个方法之后，但是返回给DispatcherServlet之前。调用该方法
	 */

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("===========拦截器方法结束end=============");
		super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * 某个方法执行完之后，做信息的清理
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("===========整个结束finally=============");
		super.afterCompletion(request, response, handler, ex);
	}

}
